package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.ICheckHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.CheckHouseGoodsSnVo;
import pers.cj.pdos.vo.CheckHouseGoodsVo;
import pers.cj.pdos.vo.CheckHouseVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class CheckHouseServiceImpl extends ServiceImpl<CheckHouseMapper, CheckHouse> implements ICheckHouseService {
    @Autowired
    CheckHouseMapper checkHouseMapper;
    @Autowired
    CheckHouseGoodsMapper checkHouseGoodsMapper;
    @Autowired
    CheckHouseGoodsSnMapper checkHouseGoodsSnMapper;
    @Autowired
    GoodsSnMapper goodsSnMapper;
    @Autowired
    GoodsSnLogMapper goodsSnLogMapper;
    @Autowired
    GoodsStockMapper goodsStockMapper;

    @Override
    public List<CheckHouseVo> listVo(String companyId,
                                     Integer page,
                                     Integer limit,
                                     Boolean close,
                                     String search,
                                     String no,
                                     String goodsName,
                                     String remark,
                                     Date makeTime1,
                                     Date makeTime2,
                                     String storeId,
                                     String userStoreId,
                                     String userId,
                                     Date date1,
                                     Date date2,
                                     String houseId,
                                     String makeUserId){
        return checkHouseMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                close,
                search,
                no,
                goodsName,
                remark,
                makeTime1,
                makeTime2,
                storeId,
                userStoreId,
                userId,
                date1,
                date2,
                houseId,
                makeUserId);
    }

    @Override
    public long count(String companyId,
                      Boolean close,
                      String search,
                      String no,
                      String goodsName,
                      String remark,
                      Date makeTime1,
                      Date makeTime2,
                      String storeId,
                      String userStoreId,
                      String userId,
                      Date date1,
                      Date date2,
                      String houseId,
                      String makeUserId){
        return checkHouseMapper.count(companyId,
                close,
                search,
                no,
                goodsName,
                remark,
                makeTime1,
                makeTime2,
                storeId,
                userStoreId,
                userId,
                date1,
                date2,
                houseId,
                makeUserId);
    }

    @Override
    public CheckHouseVo getVoById(String id) {
        return checkHouseMapper.getVoById(id);
    }

    @Override
    public boolean checkNo(String no, String companyId) {
        return checkHouseMapper.selectCount(new QueryWrapper<CheckHouse>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public boolean checkClose(CheckHouse checkHouse) {
        return checkHouseMapper.selectCount(new QueryWrapper<CheckHouse>().eq("id",checkHouse.getId()).eq("close",true))  > 0;
    }

    @Override
    public void insert(CheckHouse checkHouse, List<CheckHouseGoods> checkHouseGoodsList,
                       List<CheckHouseGoodsSn> checkHouseGoodsSnList, List<GoodsSn> addGoodsSnList,
                       List<GoodsSn> delGoodsSnList, List<GoodsSnLog> goodsSnLogList) {
        checkHouseMapper.insert(checkHouse);
        checkHouseGoodsMapper.batchInsert(checkHouseGoodsList);

        if (!Utils.isEmpty(checkHouseGoodsSnList)) {
            checkHouseGoodsSnMapper.batchInsert(checkHouseGoodsSnList);
        }

        //插入新增的sn
        if (!Utils.isEmpty(addGoodsSnList)) {
            goodsSnMapper.insertBatch(addGoodsSnList);
        }

        //出库删除的sn
        if (!Utils.isEmpty(delGoodsSnList)) {
            goodsSnMapper.outByList(checkHouse.getHouseId(), delGoodsSnList);
        }

        if (!Utils.isEmpty(goodsSnLogList)) {
            goodsSnLogMapper.insertBatch(goodsSnLogList);
        }

        //库存
        List<GoodsStock> goodsStockList = new ArrayList<>();
        for (CheckHouseGoods checkHouseGoods : checkHouseGoodsList) {
            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setId(UuidUtils.getUUID());
            goodsStock.setCompanyId(checkHouse.getCompanyId());
            goodsStock.setGoodsId(checkHouseGoods.getGoodsId());
            goodsStock.setHouseId(checkHouse.getHouseId());
            goodsStock.setNum(checkHouseGoods.getNumber());
            goodsStock.setTime(new Date());
            goodsStockList.add(goodsStock);
        }
        goodsStockMapper.insertBatchForCheckHouse(goodsStockList);
    }

    @Override
    public void close(CheckHouse checkHouse) {
        CheckHouseVo checkHouseVo = checkHouseMapper.getVoById(checkHouse.getId());
        if (checkHouseVo==null) {
            return;
        }

        List<GoodsSn> addGoodsSnList = new ArrayList<>();//已经被新的sn
        List<GoodsSn> delGoodsSnList = new ArrayList<>();//已经被删除的sn

        List<GoodsStock> goodsStockList = new ArrayList<>();
        for (CheckHouseGoodsVo checkHouseGoodsVo : checkHouseVo.getCheckHouseGoodsVoList()) {
            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setId(UuidUtils.getUUID());
            goodsStock.setCompanyId(checkHouseVo.getCompanyId());
            goodsStock.setGoodsId(checkHouseGoodsVo.getGoodsId());
            goodsStock.setHouseId(checkHouseVo.getHouseId());
            goodsStock.setNum(checkHouseGoodsVo.getDelNumber().subtract(checkHouseGoodsVo.getAddNumber()));//库存的数量变化是盘亏的数量减去盘盈的数量
            goodsStock.setTime(new Date());
            goodsStockList.add(goodsStock);

            if (!Utils.isEmpty(checkHouseGoodsVo.getCheckHouseGoodsSnVoList())){
                for (CheckHouseGoodsSnVo snVo : checkHouseGoodsVo.getCheckHouseGoodsSnVoList()) {
                    if (snVo.getStatus() == 2) {
                        addGoodsSnList.add(snVo.getGoodsSn());
                    } else if (snVo.getStatus() == 3) {
                        delGoodsSnList.add(snVo.getGoodsSn());
                    }
                }
            }
        }

        //删除的sn-作废时要再入库
        if (!Utils.isEmpty(delGoodsSnList)) {
            goodsSnMapper.inByList(checkHouse.getHouseId(), delGoodsSnList);
        }
        //新增的sn，作废时要出库
        if (!Utils.isEmpty(addGoodsSnList)) {
            goodsSnMapper.outByList(checkHouse.getHouseId(), addGoodsSnList);
        }
        goodsStockMapper.insertBatch(goodsStockList);


        CheckHouse temp = new CheckHouse();
        temp.setId(checkHouseVo.getId());
        temp.setClose(true);
        checkHouseMapper.updateById(temp);
    }
}
